<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add">新增快递公司</el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-select style="width: 130px;" v-model="search.type" clearable placeholder="选择类型" filterable>
						<el-option v-for="(item, key) in types" :key="key" :label="item" :value="key" />
					</el-select>
					<el-select style="width: 130px;" v-model="search.status" clearable placeholder="选择状态" filterable>
						<el-option v-for="(item, key) in statuss" :key="key" :label="item" :value="key" />
					</el-select>
					<el-input style="width: 220px;" v-model="search.keyword" placeholder="请输入公司名称或代码" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch">搜索</el-button>
					<el-button icon="el-icon-refresh" @click="resetSearch">重置</el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="list.apiObj" row-key="id" stripe>
				<el-table-column label="类型" prop="type" width="80">
					<template #default="scope">
						<p>{{types[scope.row.type]}}</p>
					</template>
				</el-table-column>
				<el-table-column label="公司代码" prop="code" width="150"></el-table-column>
				<el-table-column label="公司名字" prop="company_name" width="150"></el-table-column>
				
				<el-table-column label="图标" width="80">
					<template #default="scope">
						<el-image :src="scope.row.icon_url" size="small"></el-image>
					</template>
				</el-table-column>
				<el-table-column label="首重利润率" prop="rate" width="150"></el-table-column>
				<el-table-column label="续重利润率" prop="rate_over" width="150"></el-table-column>
				<el-table-column label="备注" prop="remark" width="150"></el-table-column>
				<el-table-column label="排序" prop="sort" width="150"></el-table-column>
				<el-table-column label="状态" prop="status" width="80">
					<template #default="scope">
						<p>{{statuss[scope.row.status]}}</p>
					</template>
				</el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="150">
					<template #default="scope">
						<el-button type="primary" plain size="small" @click="table_edit(scope.row)">编辑</el-button>
						<!-- <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
							<template #reference>
								<el-button text type="primary" size="small">删除</el-button>
							</template>
						</el-popconfirm> -->
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>

</template>

<script>
	import saveDialog from './save'
	export default {
		name: 'configureList',
		components: {
			saveDialog,
		},
		data() {
			return {
				search: {
					type: '',
					status: '',
					keyword: '',
				},
				dialog:{
					save: false,
				},
				list: {
					apiObj: this.$API.expressCompany.expressCompany.list
				},
				statuss: {'0': '未启用', '1': '启用'},
				types: {'1': '快递', '3': '同城', '4': '物流'},
			}
		},
		mounted() {

		},
		methods: {
			//搜索
			upsearch() {
				let search = JSON.parse(JSON.stringify(this.search))
				this.$refs.table.upData({
					...search
				})
			},
			//删除
			async table_del(row){
				var reqData = {id: row.id}
				var res = await this.$API.expressCompany.expressCompany.delete.post(reqData);
				if(res.code == 200){
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				}else{
					this.$alert(res.msg, "提示", {type: 'error'})
				}
			},
			//添加
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//窗口编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//重置搜索
			resetSearch() {
				this.search = {
					type: '',
					status: '',
					keyword: '',
				}
				this.upsearch()
			},

			//本地更新数据
			handleSaveSuccess(data, mode){
				//为了减少网络请求，直接变更表格内存数据
				if(mode=='add'){
					this.$refs.table.unshiftRow(data)
				}else if(mode=='edit'){
					this.$refs.table.updateKey(data)
				}

				//当然也可以暴力的直接刷新表格
				// this.$refs.table.refresh()
			}
		}
	}
</script>

<style>
.right-panel-search {
	display: flex;
	align-items: center;
	gap: 12px;
}

.left-panel {
	display: flex;
	align-items: center;
}

.el-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 20px 24px;
	background: #fff;
	border-bottom: 1px solid #ebeef5;
	box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.right-panel-search .el-select {
	margin-right: 8px;
}

.right-panel-search .el-input {
	margin-right: 8px;
}

.right-panel-search .el-button {
	border-radius: 6px;
	font-weight: 500;
}

.right-panel-search .el-button:not(.el-button--primary) {
	border-color: #dcdfe6;
	color: #606266;
}

.right-panel-search .el-button:not(.el-button--primary):hover {
	border-color: #409eff;
	color: #409eff;
}

.left-panel .el-button {
	border-radius: 6px;
	font-weight: 500;
	padding: 12px 20px;
}

.el-main {
	background: #f5f7fa;
	padding: 20px;
}

.el-table {
	border-radius: 8px;
	box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}
</style>
